package com.glf.tool;

import com.alibaba.fastjson.JSONObject;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ImageInfoUtil {

    public static void main(String[] args) {
        new ImageInfoUtil().fileGpstext3();

//        try {
//            PicAnalysis("C:\\Users\\priva\\Desktop\\微信图片_20240808171427.jpg");
//        } catch (JpegProcessingException e) {
//            throw new RuntimeException(e);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
    }

    //分析图片获取地址
    public static Map<String, String> PicAnalysis(String path) throws JpegProcessingException, IOException {
        Map<String, String> map = new HashMap();
        File picFile = new File(path);
        Metadata metadata = JpegMetadataReader.readMetadata(picFile);
        Iterator<Directory> it = metadata.getDirectories().iterator();
        while (it.hasNext()) {
            Directory exif = it.next();
            Iterator<Tag> tags = exif.getTags().iterator();
            while (tags.hasNext()) {
                Tag tag = tags.next();
                map.put(tag.getTagName(), tag.getDescription());
                System.out.println(tag.getTagName() + ":" + tag.getDescription());
            }
        }
        System.out.println("ok！");
        return map;
    }


    public void fileGpstext3() {
        File file = new File("C:\\Users\\priva\\Desktop\\微信图片_20240808171427.jpg");
        JSONObject jsonObject = getPosition(file);
        System.out.println(jsonObject);
//        Map map = getAddress(jsonObject);

        String gps_longitude = jsonObject.getString("GPS Longitude");//经度
        String gps_latitude = jsonObject.getString("GPS Latitude");//纬度
        double longitude = transformPosition(gps_longitude);
        double latitude = transformPosition(gps_latitude);
        System.out.println("经度：" + longitude + "   纬度：" + latitude);
        System.out.println("手机型号：" + jsonObject.getString("Make") + "  " + jsonObject.getString("Model"));
        System.out.println("拍摄时间：" + jsonObject.getString("Date/Time"));
//        System.out.println("拍摄地址："+map.get("formatted_address")+" "+map.get("sematic_description"));
    }

    private static JSONObject getPosition(File file) {
        JSONObject jsonObject = new JSONObject();
        Metadata metadata = null;
        try {
            metadata = JpegMetadataReader.readMetadata(file);
            for (Directory directory : metadata.getDirectories()) {
                for (Tag tag : directory.getTags()) {
                    jsonObject.put(tag.getTagName(), tag.getDescription());
                }
            }
        } catch (JpegProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    /**
     * 经纬度转换
     *
     * @param s
     * @return
     */
    private static double transformPosition(String s) {
        String a = s.split("°")[0].replace(" ", "");
        String b = s.split("°")[1].split("'")[0].replace(" ", "");
        String c = s.split("°")[1].split("'")[1].replace(" ", "").replace("\"", "");
        double gps_dou = Double.parseDouble(a) + Double.parseDouble(b) / 60 + Double.parseDouble(c) / 60 / 60;
        return gps_dou;
    }
    /**
     *  经纬度定位结果（百度地图）
     * @param
     * @param
     * @return
     */
//    private static Map getAddress(JSONObject json) {
//        String log = String.valueOf(transformPosition(json.getString("GPS Longitude")));//经度
//        String lat = String.valueOf(transformPosition(json.getString("GPS Latitude")));//纬度
//        String ak = "自己的百度应用ak";
//        String urlString =
//                "http://api.map.baidu.com/reverse_geocoding/v3/?ak=" + ak +
//                        "&output=json&coordtype=wgs84ll&location=" + lat + "," + log;
//        Map map = new LinkedHashMap();
//        try {
//            URL url = new URL(urlString);
//            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//
//            if (conn != null) {
//                InputStreamReader inputStreamReader = new InputStreamReader(conn.getInputStream(), "UTF-8");
//                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//                String data = null;
//                if ((data = bufferedReader.readLine()) != null) {
//                    JSONObject jsonObject = JSON.parseObject(data);
//                    Object result = jsonObject.get("result");
//                    map = JSONObject.parseObject(JSONObject.toJSONString(result), Map.class);
//                }
//                inputStreamReader.close();
//            }
//        } catch (Exception e) {
//            System.out.println("error in wapaction,and e is " + e.getMessage());
//        }
//        return map;
//    }


}
